//
//  FeaturesTableViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-4-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FeaturesTableViewController.h"
#import "TechnologyViewController.h"
#import "MediaPlayer/MPMoviePlayerController.h"
#import "MyMoviePlayerViewController.h"
#import "PhotoGalleryViewController.h"

@interface FeaturesTableViewController ()

@end

@implementation FeaturesTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        //self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
        
        self.title = NSLocalizedString(@"Features", @"Features");
        self.tabBarItem.image = [UIImage imageNamed:@"96-book"];
        
        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"Back";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        
        self.tableView.separatorColor = [UIColor clearColor];
        self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-black.png"]];
        NSArray *section1 = [NSArray arrayWithObjects: @"NCG Aircon Inverter Technology", @"ECONAVI Technology",@"Mild Dry Cooling", @"e-ion Air Purification", nil];
        NSArray *section2 = [NSArray arrayWithObjects: @"NCG Aircon Inverter Technology", @"ECONAVI Technology", @"Mild Dry Cooling",@"e-ion Air Purification", nil];
        NSArray *section3 = [NSArray arrayWithObjects: @"View Photos", nil];
        
        titles = [NSArray arrayWithObjects: section1, section2, section3, nil];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3; 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 4;
            break;
        case 1:
            return 4;
            break; 
        case 2:
            return 1;
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString* cellID = @"FeaturesTableViewCellID";
    cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
        cell.textLabel.backgroundColor= [UIColor clearColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-row-basics.png"]];
        UIImageView *highlightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-row-basicsb.png"]];
        cell.backgroundView = imageView;
        cell.selectedBackgroundView = highlightImageView;
    }
    NSString *title = (NSString*)[(NSArray*)[titles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = title;
    
    
    
    return cell;
}




#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    headerView.backgroundColor = [UIColor colorWithRed:0.93 green:0.07 blue:0.81 alpha:1];     
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-row-subhdr.png"]];
    imageView.frame = CGRectMake(0, 0, tableView.bounds.size.width, 22);
    [headerView addSubview:imageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    switch (section) {
        case 0:
            label.text = @"   VIDEOS";
            break;
        case 1:
            label.text = @"   NCG AIRCON TECHNOLOGY ";
            break;
        case 2:
            label.text = @"   PHOTO GALLERY";
            break;
        default:
            break;
    }
    label.font = [UIFont boldSystemFontOfSize:11];
    // you can set the alignment if you want, default value is UITextAlignmentLeft
    label.textAlignment = UITextAlignmentLeft;
    // set the text colour
    label.textColor = [UIColor whiteColor];
    // set the label background colour to be transparent
    label.backgroundColor = [UIColor clearColor];
    
    // add label to the header
    [headerView addSubview:label];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

/*
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self rowSelected:indexPath];
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self rowSelected:indexPath];
}

- (void)rowSelected:(NSIndexPath *)indexPath
{
    switch (indexPath.section) { 
        case 0:{
            inverterVideoController = [self createVideoController:@"4-mild-dry"];
            
            // Register for the playback finished notification.
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(myMovieFinishedCallback:)
                                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                                       object:inverterVideoController.moviePlayer];
            
            inverterVideoController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
            [self presentMoviePlayerViewControllerAnimated:inverterVideoController];
            [inverterVideoController.moviePlayer play];
            NSLog(@"played");
            break;
        }
        case 1:{
            if (inverterTechViewController == nil) {
                inverterTechViewController = [[TechnologyViewController alloc] initWithNibName:@"TechnologyViewController" bundle:nil]; 
            }
            [self.navigationController pushViewController:inverterTechViewController animated:YES];
            break;
        }
        case 2:{
            if(photoGalleryViewController == nil){
                photoGalleryViewController = [[PhotoGalleryViewController alloc] initWithNibName:@"PhotoGalleryViewController" bundle:nil]; 
            }
            
            
            [self.navigationController pushViewController:photoGalleryViewController animated:YES];
        }
        default:
            break;
    }
}

- (MyMoviePlayerViewController*) createVideoController:(NSString*)fileName
{
    NSString *moviePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
    NSURL *theMovieURL = [NSURL fileURLWithPath:moviePath];

    MyMoviePlayerViewController *moviePlayerView = [[MyMoviePlayerViewController alloc] initWithContentURL:theMovieURL]; 
   // [moviePlayerView.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    //moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    //[moviePlayerView.moviePlayer setFullscreen:YES]; 
    //moviePlayerView.view.transform =CGAffineTransformMakeRotation((M_PI / 2.0));
    return moviePlayerView;
}

// When the movie is done,release the controller.
- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{
    NSLog(@"myMovieFinishedCallback");
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:[aNotification object]];
    [self dismissMoviePlayerViewControllerAnimated];
    inverterVideoController = nil;
} 

@end
